;; Copyright © 2017, 2018 Kei Kebreau <kkebreau@posteo.net>

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

(define-module (misc-utils)
  #:use-module (guix packages)
  #:use-module (gnu packages)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix download)
  #:use-module (guix git-download)
  #:use-module (guix utils)
  #:use-module (guix build-system gnu)
  #:use-module (guix build-system waf)
  #:use-module (gnu packages gettext)
  #:use-module (gnu packages gl)
  #:use-module (gnu packages glib)
  #:use-module (gnu packages gnome)
  #:use-module (gnu packages gtk)
  #:use-module (gnu packages image)
  #:use-module (gnu packages linux)
  #:use-module (gnu packages messaging)
  #:use-module (gnu packages pkg-config)
  #:use-module (gnu packages python)
  #:use-module (gnu packages xml)
  #:use-module (gnu packages xorg))

(define-public rox-filer
  (package
    (name "rox-filer")
    (version "2.11")
    (source (origin
              (method url-fetch)
              (uri (string-append "https://sourceforge.net/projects/rox/files/"
                                  "rox/" version "/"
                                  name "-" version ".tar.bz2"))
              (sha256
               (base32
                "1a0v21y3srmz0ikg62df6hpc94j5axs1b5wbshp7mvqqxqrbsad9"))))
    (build-system gnu-build-system)
    (arguments
     `(#:tests? #f
       #:phases
       (modify-phases %standard-phases
         (replace 'configure
           (lambda* (#:key outputs #:allow-other-keys)
             ;; Replace /bin/sh with the appropriate location of sh.
             (substitute* '("ROX-Filer/src/main.c"
                            "ROX-Filer/src/type.c")
               (("/bin/sh") (which "sh")))
             ;; Make sure that rox-filer looks for its private files in its
             ;; share directory.
             (substitute* "ROX-Filer/src/main.c"
               (("g_strdup\\(getenv\\(\"APP_DIR\"\\)\\)")
                (string-append "\"" (assoc-ref outputs "out") "/share/rox\"")))
             #t))
         (replace 'build
           (lambda _
             (setenv "CONFIG_SHELL" (which "sh"))
             (setenv "LIBS" "-ldl -lm")
             (invoke "ROX-Filer/AppRun" "--compile")))
         ;; Installation steps are taken from Linux From Scratch.
         (replace 'install
           (lambda* (#:key outputs #:allow-other-keys)
             (let* ((out (assoc-ref outputs "out"))
                    (share (string-append out "/share/rox"))
                    (bin (string-append out "/bin"))
                    (man1 (string-append out "/share/man/man1")))
               (mkdir-p man1)
               (install-file "rox.1" man1)
               (chdir "ROX-Filer")
               (mkdir-p share)
               (for-each (lambda (file)
                           (copy-recursively file
                                             (string-append share "/" file)))
                         '("Help" "Messages" "Options.xml" "ROX" "images"
                           "style.css" ".DirIcon"))
               (mkdir-p bin)
               (copy-file "ROX-Filer" (string-append bin "/rox"))
               (chdir (string-append share "/ROX/MIME"))
               ;; Duplicate some common mime types, without which rox-filer
               ;; would display the "unknown binary blob" icon.
               (symlink "text-x-diff.png" "text-x-patch.png")
               (symlink "application-x-font-afm.png"
                        "application-x-font-type1.png")
               (symlink "application-xml.png" "application-xml-dtd.png")
               (symlink "application-xml.png"
                        "application-xml-external-parsed-entity.png")
               (symlink "application-xml.png" "application-rdf+xml.png")
               (symlink "application-xml.png" "application-x-xbel.png")
               (symlink "application-x-shellscript.png"
                        "application-javascript.png")
               (symlink "application-x-bzip-compressed-tar.png"
                        "application-x-xz-compressed-tar.png")
               (symlink "application-x-bzip-compressed-tar.png"
                        "application-x-lzma-compressed-tar.png")
               (symlink "application-x-bzip-compressed-tar.png"
                        "application-x-lzo.png")
               (symlink "application-x-bzip.png" "application-x-xz.png")
               (symlink "application-x-gzip.png" "application-x-lzma.png")
               (symlink "application-msword.png" "application-rtf.png")))))))
    (native-inputs
     `(("pkg-config" ,pkg-config)))
    (inputs
     `(("gtk+-2" ,gtk+-2)
       ("libsm" ,libsm)
       ("libxml2" ,libxml2)))
    (home-page "https://launchpad.net/sakura")
    (synopsis "A RISC OS-like filer for Xorg")
    (description "ROX-Filer is a simple and easy to use graphical file manager
for X11 based on the GTK2 library.  It uses a uniform drag-and-drop approach for
every operation.")
    (license license:gpl2+)))
